// JavaScript Document
function success(position) {
	
	var s = document.querySelector('#status');
	s.innerHTML = "Finding your location: found you!";
	window.location.replace("storechoose.html?lat="+position.coords.latitude+"&lng="+position.coords.longitude);
}
 
function error(msg) {
	var s = document.querySelector('#status');
	s.innerHTML = typeof msg == 'string' ? msg : "We're really sorry, but something went very wrong when trying to locate you. Please, use the button below to specify your location manually. Pretty please ?";
	s.className = 'fail';
}
 
function html5Locate(){
	var s = document.querySelector('#status');
	s.innerHTML = "Finding your location: checking...";
	if (navigator.geolocation) {
		navigator.geolocation.getCurrentPosition(success, error);
	} else {
		error('not supported');
	}
}

function zip(){
	window.location.replace("zipcode.html");
}

function backToMain(){
	window.location.replace("index.html");
}

function goToStoreView(storeID){
	var storeName;
	switch(storeID) {
		case 1:
			storeName = "Safeway";
			break;
		case 2:
			storeName = "Safeway";
			break;
		case 3:
			storeName = "Target";
			break;
		case 4:
			storeName = "Trader Joe's";
			break;
		case 5:
			storeName = "Ikea";
			break;
		default:
			storeName = "DefaultStore1";
			break;
	}
	window.location.replace("main_menu.php?store="+storeName);
}

// See http://code.google.com/apis/maps/documentation/javascript/reference.html#LatLng for explanations
function translateZipCode(){
	var zip = document.forms["zipcode"].elements["zip"].value;
	geocoder = new google.maps.Geocoder();
	geocoder.geocode( { 'address': zip}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        //alert(results[0].geometry.location.lat() + " " + results[0].geometry.location.lng());
		window.location.replace("storechoose.html?lat="+results[0].geometry.location.lat()+"&lng="+results[0].geometry.location.lng());
      } else {
        alert("We're sorry, we could not match this code to an actual location: " + status);
      }
    });
}

function outputDistances(){
	var lat = getQuerystring('lat');
	var lng = getQuerystring('lng');
	//Safeway 1: 37.476357,-122.231655
	var s = document.querySelector('#store1');
	s.innerHTML = "<input class=\"goToStoreMenubutton\" type=\"button\" id=\"goToStoreMenubutton\" value=\"Go to Safeway (1)\" onclick=\"goToStoreView(1)\" style=\"visibility: visible;\"> Distance : " + findDistance(lat, lng, 37.476357, -122.231655) + " km";
	
	//Safeway 2: 37.476357,-122.241096
	s = document.querySelector('#store2');
	s.innerHTML = "<input class=\"goToStoreMenubutton\" type=\"button\" id=\"goToStoreMenubutton\" value=\"Go to Safeway (2)\" onclick=\"goToStoreView(2)\" style=\"visibility: visible;\"> Distance : " + findDistance(lat, lng, 37.476357,-122.241096) + " km";
	
	//Target 1: 37.476357,-122.220497
	s = document.querySelector('#store3');
	s.innerHTML = "<input class=\"goToStoreMenubutton\" type=\"button\" id=\"goToStoreMenubutton\" value=\"Go to Target (1)\" onclick=\"goToStoreView(3)\" style=\"visibility: visible;\"> Distance : " + findDistance(lat, lng, 37.476357,-122.220497) + " km";

	//Trader's Joe 1: 37.481669,-122.239723
	s = document.querySelector('#store4');
	s.innerHTML = "<input class=\"goToStoreMenubutton\" type=\"button\" id=\"goToStoreMenubutton\" value=\"Go to Trader's Joe (1)\" onclick=\"goToStoreView(4)\" style=\"visibility: visible;\"> Distance : " + findDistance(lat, lng, 37.481669,-122.239723) + " km";

	//Ikea 1: 38.009148,-122.845001
	s = document.querySelector('#store5');
	s.innerHTML = "<input class=\"goToStoreMenubutton\" type=\"button\" id=\"goToStoreMenubutton\" value=\"Go to Ikea (1)\" onclick=\"goToStoreView(5)\" style=\"visibility: visible;\"> Distance : " + findDistance(lat, lng, 38.009148,-122.845001) + " km";
	
	
}

function findDistance(lat1, lon1, lat2, lon2){
	var R = 6371; // km
	var dLat = (lat2-lat1)* Math.PI / 180;
	var dLon = (lon2-lon1)* Math.PI / 180;
		lat1 = lat1* Math.PI / 180;
		lat2 = lat2* Math.PI / 180;
	var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
        	Math.cos(lat1) * Math.cos(lat2) * 
        	Math.sin(dLon/2) * Math.sin(dLon/2); 
	var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
	var d = Math.round((R * c)*100)/100;
	return d;
}

function getQuerystring(key, default_)
{
  if (default_==null) default_=""; 
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
    return default_;
  else
    return qs[1];
}